package tutorial.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectIO {

    private static final String DATA_FILE  = "src/tutorial/io/object.txt";
    
    public static void main(String[] args) {
//        writerToFile(DATA_FILE);
        readFromFile(DATA_FILE);
    }
    
    public static void writerToFile(String dataFile) {
        SeriObject obj = new SeriObject();
        ObjectOutputStream writer = null;
        try {
            writer = new ObjectOutputStream(new FileOutputStream(dataFile));
            writer.writeObject(obj);
            writer.writeObject(obj);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void readFromFile(String dataFile) {
        ObjectInputStream reader = null;
        try {
            reader = new ObjectInputStream(new FileInputStream(dataFile));
            Object obj1 = reader.readObject();
            Object obj2 = reader.readObject();
            if (obj1 == obj2) {
                System.out.println("The two objects are the same");
            } else {
                System.out.println("The two objects are NOT the same");
            }
        } catch (IOException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}
